package com.santiao.youxiang.ui.adapter

import android.annotation.SuppressLint
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.santiao.youxiang.bean.habitTimeLineBean
import com.santiao.youxiang.R
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
//import com.caipudaquan.entity.FoodSearchResbean


class TimeLineAdapter(data: MutableList<habitTimeLineBean>? = null) :
    BaseQuickAdapter<habitTimeLineBean, BaseViewHolder>(R.layout.item_time_linel, data) {

    init {
        // 注册需要监听点击的子视图 ID
//        addChildClickViewIds(R.id.ck_check)
    }

    @SuppressLint("NewApi")
    override fun convert(holder: BaseViewHolder, item: habitTimeLineBean) {
        // 绑定数据到视图
        Log.e("convert:创建时间 ", item.date.toString())
//        holder.setText(R.id.tv_titles, item.name)
//
//        // 获取 CheckBox 并绑定选中状态
//        val checkBox: CheckBox = holder.getView(R.id.ck_check)
//        checkBox.isChecked = item.isChecked

        // 监听 CheckBox 选中状态变化
//        checkBox.setOnCheckedChangeListener { _, isChecked ->
//            item.isChecked = isChecked // 更新数据模型
//            // 可在此处触发其他逻辑（如更新总价）
////           notifyDataSetChanged()
//        }

        var time =item.date

        val formatter = DateTimeFormatter.ofPattern("yyyy-M-d H:m:s")

        // 解析时间字符串为 LocalDateTime
        val dateTime = LocalDateTime.parse(time, formatter)

        // 获取星期几
        val dayOfWeek = dateTime.dayOfWeek.value
//        val dayOfWeekString = dayOfWeek.name
        holder.setText(R.id.tv_day, dateTime.dayOfMonth.toString())
        holder.setText(R.id.tv_mounth, dateTime.month.value.toString()+"月")
        holder.setText(R.id.tv_week, "周"+dayOfWeek)



        // 初始化内层 RecyclerView
        val rvMater: RecyclerView = holder.getView(R.id.rv_detail)
        rvMater.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)


        // 设置内层适配器
        val materialAdapter = TimeDetailAdapter(item.detail as MutableList<habitTimeLineBean.Detail>)
        rvMater.adapter = materialAdapter
//        materialAdapter.data.clear()
//        materialAdapter.setList(item.detail as MutableList<habitTimeLineBean.Detail>)
        // 可选：禁用嵌套滚动（解决滑动冲突）
        rvMater.isNestedScrollingEnabled = false
        Log.e("convert:名称 ", item.toString())

//        holder.setText(R.id.tv_num, item.portionSize)

//        var image=holder.getView<ImageView>(R.id.iv_photo)

//        GlideApp.with(this)        .load(R.raw.large_giphy_logo)        .into(giphyLogoView);

//        Glide.with(holder.itemView.context).load(item.imageUrl).into(image)
//        holder.setText(R.id.tv_content, item.content)
    }

//    fun toggleAll(selectAll: Boolean) {
//        data?.forEach { it.isChecked = selectAll }
//        notifyDataSetChanged() // 刷新整个列表
//    }

}